/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Profiles.java
 *
 * Created on Aug 22, 2011, 6:31:00 PM
 */
package profiles;

import app.Global;
import app.MyTable;
import app.MyView;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JScrollPane;

/**
 *
 * @author yoshi
 */
public class ProfilesDefault extends javax.swing.JPanel implements MyView {

    private JScrollPane tableScroll = new JScrollPane();
    private MyTable table = new MyTable();
    private ProfilesModel myModel;
    private ProfilesController myController;

    /** Creates new form Profiles */
    public ProfilesDefault() {
        initComponents();

        this.txtAddress.setEnabled(false);
        this.txtMobile.setEnabled(false);
        this.txtEmail.setEnabled(false);
        this.txtAddress.setText(Global.getAuth().getAddress());
        this.txtMobile.setText(Global.getAuth().getMobile());
        this.txtEmail.setText(Global.getAuth().getEmail());
        this.lblFullTitle.setText(Global.getAuth().getFullTitle());
        this.lblRoll.setText(Global.getAuth().getFullRollName());

        this.setSize(880, 600);
        Global.getFrame().setTitle("Profile");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel3 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        btnChangePassword = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        lblDepartmentName = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        txtEmail = new javax.swing.JTextField();
        btnChangeEmail = new javax.swing.JButton();
        jLabel11 = new javax.swing.JLabel();
        txtMobile = new javax.swing.JTextField();
        btnChangeMobile = new javax.swing.JButton();
        jLabel12 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtAddress = new javax.swing.JTextArea();
        btnChangeAddress = new javax.swing.JButton();
        lblRoll = new javax.swing.JLabel();
        lblFullTitle = new javax.swing.JLabel();

        setPreferredSize(new java.awt.Dimension(880, 600));
        setLayout(new java.awt.BorderLayout());

        jPanel3.setBackground(java.awt.Color.white);

        jPanel2.setBackground(java.awt.Color.white);
        jPanel2.setPreferredSize(new java.awt.Dimension(700, 600));

        jLabel1.setText("Welcome,  ");

        btnChangePassword.setText("Change password");
        btnChangePassword.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnChangePasswordActionPerformed(evt);
            }
        });

        jLabel5.setText("Roll");

        jLabel9.setText("Contact details:");

        jLabel10.setText("Email:");

        btnChangeEmail.setText("Change");
        btnChangeEmail.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnChangeEmailActionPerformed(evt);
            }
        });

        jLabel11.setText("Mobile:");

        btnChangeMobile.setText("Change");
        btnChangeMobile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnChangeMobileActionPerformed(evt);
            }
        });

        jLabel12.setText("Address:");

        txtAddress.setColumns(20);
        txtAddress.setRows(5);
        jScrollPane1.setViewportView(txtAddress);

        btnChangeAddress.setText("Change");
        btnChangeAddress.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnChangeAddressActionPerformed(evt);
            }
        });

        lblRoll.setText("jLabel2");

        lblFullTitle.setText("jLabel2");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(96, 96, 96)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel9)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(txtEmail))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel11, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel12))
                                .addGap(18, 18, 18)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(txtMobile)))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel5)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel2Layout.createSequentialGroup()
                                        .addGap(94, 94, 94)
                                        .addComponent(lblDepartmentName))
                                    .addGroup(jPanel2Layout.createSequentialGroup()
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(lblRoll, javax.swing.GroupLayout.DEFAULT_SIZE, 296, Short.MAX_VALUE))))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(lblFullTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 281, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btnChangeEmail, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnChangePassword)
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(btnChangeAddress, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(btnChangeMobile, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 91, Short.MAX_VALUE)))))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(btnChangePassword)
                    .addComponent(lblFullTitle))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(lblDepartmentName)
                    .addComponent(lblRoll))
                .addGap(18, 18, 18)
                .addComponent(jLabel9)
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnChangeEmail))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel11)
                    .addComponent(txtMobile, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnChangeMobile))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel12)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnChangeAddress))
                .addContainerGap(231, Short.MAX_VALUE))
        );

        jPanel3.add(jPanel2);

        add(jPanel3, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

private void btnChangeEmailActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnChangeEmailActionPerformed
    if (this.txtEmail.isEnabled()) {
        this.txtEmail.setEnabled(false);
        this.btnChangeEmail.setText("Change");

        new Thread() {

            @Override
            public void run() {
                Connection connection = Global.getConnection();
                PreparedStatement pst = null;
                try {
                    pst = connection.prepareStatement("UPDATE employees SET email = ? WHERE id = ?");
                    pst.setString(1, txtEmail.getText());
                    pst.setLong(2, Global.getAuth().getId());
                    if (pst.executeUpdate() > 0) {
                        Global.getAuth().setEmail(txtEmail.getText());
                        Global.notify("Update success", "Your email has been updated successfully", WIDTH);
                    }
                } catch (SQLException ex) {
                    Logger.getLogger(ProfilesDefault.class.getName()).log(Level.SEVERE, null, ex);
                } finally {
                    try {
                        pst.close();
                        connection.close();
                    } catch (SQLException ex) {
                        Logger.getLogger(ProfilesDefault.class.getName()).log(Level.SEVERE, null, ex);
                        Global.notify("Database error", "There's problems with the database connection, please try again later", WIDTH);
                    } finally {
                        txtEmail.setText(Global.getAuth().getEmail());
                    }
                }
            }
        }.start();
    } else {
        this.txtEmail.setEnabled(true);
        this.btnChangeEmail.setText("Save");
    }

}//GEN-LAST:event_btnChangeEmailActionPerformed

private void btnChangeMobileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnChangeMobileActionPerformed
    if (this.txtMobile.isEnabled()) {
        this.txtMobile.setEnabled(false);
        this.btnChangeMobile.setText("Change");

        new Thread() {

            @Override
            public void run() {
                Connection connection = Global.getConnection();
                PreparedStatement pst = null;
                try {
                    pst = connection.prepareStatement("UPDATE employees SET mobile = ? WHERE id = ?");
                    pst.setString(1, txtMobile.getText());
                    pst.setLong(2, Global.getAuth().getId());
                    if (pst.executeUpdate() > 0) {
                        Global.getAuth().setMobile(txtMobile.getText());
                        Global.notify("Update success", "Your mobile has been updated successfully", WIDTH);
                    }
                } catch (SQLException ex) {
                    Logger.getLogger(ProfilesDefault.class.getName()).log(Level.SEVERE, null, ex);
                    Global.notify("Database error", "There's problems with the database connection, please try again later", WIDTH);
                } finally {
                    try {
                        pst.close();
                        connection.close();
                    } catch (SQLException ex) {
                        Logger.getLogger(ProfilesDefault.class.getName()).log(Level.SEVERE, null, ex);
                    } finally {
                        txtMobile.setText(Global.getAuth().getMobile());
                    }
                }
            }
        }.start();
    } else {
        this.txtMobile.setEnabled(true);
        this.btnChangeMobile.setText("Save");
    }
}//GEN-LAST:event_btnChangeMobileActionPerformed

private void btnChangeAddressActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnChangeAddressActionPerformed
    if (this.txtAddress.isEnabled()) {
        this.txtAddress.setEnabled(false);
        this.btnChangeAddress.setText("Change");

        new Thread() {

            @Override
            public void run() {
                Connection connection = Global.getConnection();
                PreparedStatement pst = null;
                try {
                    pst = connection.prepareStatement("UPDATE employees SET address = ? WHERE id = ?");
                    pst.setString(1, txtAddress.getText());
                    pst.setLong(2, Global.getAuth().getId());
                    if (pst.executeUpdate() > 0) {
                        Global.getAuth().setAddress(txtAddress.getText());
                        Global.notify("Update success", "Your address has been updated successfully", WIDTH);
                    }
                } catch (SQLException ex) {
                    Logger.getLogger(ProfilesDefault.class.getName()).log(Level.SEVERE, null, ex);
                    Global.notify("Database error", "There's problems with the database connection, please try again later", WIDTH);
                } finally {
                    try {
                        pst.close();
                        connection.close();
                    } catch (SQLException ex) {
                        Logger.getLogger(ProfilesDefault.class.getName()).log(Level.SEVERE, null, ex);
                    } finally {
                        txtAddress.setText(Global.getAuth().getAddress());
                    }
                }
            }
        }.start();
    } else {
        this.txtAddress.setEnabled(true);
        this.btnChangeAddress.setText("Save");
    }
}//GEN-LAST:event_btnChangeAddressActionPerformed

private void btnChangePasswordActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnChangePasswordActionPerformed
    new ChangePasswordDialog(Global.getFrame(), true).setVisible(true);
}//GEN-LAST:event_btnChangePasswordActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnChangeAddress;
    private javax.swing.JButton btnChangeEmail;
    private javax.swing.JButton btnChangeMobile;
    private javax.swing.JButton btnChangePassword;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblDepartmentName;
    private javax.swing.JLabel lblFullTitle;
    private javax.swing.JLabel lblRoll;
    private javax.swing.JTextArea txtAddress;
    private javax.swing.JTextField txtEmail;
    private javax.swing.JTextField txtMobile;
    // End of variables declaration//GEN-END:variables

    @Override
    public ProfilesModel getMyModel() {
        return myModel;
    }

    @Override
    public ProfilesController getMyController() {
        return myController;
    }

    @Override
    public void refresh() {
        this.getMyModel().clear();
        tableScroll.setViewportView(table);
    }
}
